package com.example;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: dlernatovich
 * Date: 6/26/12
 * Time: 2:53 PM
 * To change this template use File | Settings | File Templates.
 */
public class SecondActivity extends Activity implements View.OnClickListener {

    EditText emailText;
    EditText messageText;
    Button sendButton;

    private Pattern pattern;
    private Matcher matcher;

    private static final String EMAIL_PATTERN =
            "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    private static final int ALERT_DIALOG_MAIL = 0x11236654;
    ArrayList<String> errors = new ArrayList<String>();


    public void onCreate(Bundle savedInstanceState) {
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        emailText = (EditText) findViewById(R.id.editEmail);
        messageText = (EditText) findViewById(R.id.editSend);
        sendButton = (Button) findViewById(R.id.sendTextButton);

        sendButton.setOnClickListener(this);

    }


    private boolean emailValidate(StringBuilder text) {
        boolean result = false;
        if (text.toString().trim().equalsIgnoreCase("")) {
            return false;
        }
        pattern = Pattern.compile(EMAIL_PATTERN);
        matcher = pattern.matcher(text.toString().trim());
        result = matcher.matches();
        return result;
    }

    private void sendMessage() {
        errors.clear();
        StringBuilder email = new StringBuilder(emailText.getText().toString().trim());
        StringBuilder message = new StringBuilder(messageText.getText().toString().trim());
        if (!emailValidate(email)) {
            errors.add("EMAIL ERROR: Email is not validate!!");
            if (message.toString().trim().equalsIgnoreCase("")) {
                errors.add("MESSAGE ERROR: Message is empty!!");
            }

            if (errors.size() == 0) {
                Log.e("SENDER", email.toString());
                Log.e("MESSAGE", message.toString());
            } else {
                showDialog(ALERT_DIALOG_MAIL);
            }
        }
    }

    private String showErrors(List<String> errors) {
        StringBuilder result = new StringBuilder();
        for (String getText : errors) {
            result.append(getText + "\n");
        }
        return result.toString().trim();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sendTextButton:
                sendMessage();
                break;
            default:
                break;
        }
    }

    protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        AlertDialog.Builder builder;
        switch (id) {
            case ALERT_DIALOG_MAIL:
                builder = new AlertDialog.Builder(this);
                builder.setMessage("Email error, correct e-mail?");
                builder.setCancelable(false);
                builder.setPositiveButton("Yes", null);
                builder.setNegativeButton("No", null);
                dialog = builder.create();
                break;
            default:
                dialog = null;
        }
        return dialog;
    }

    protected void onPrepareDialog(final int id, final Dialog dialog) {
        switch (id) {
            case ALERT_DIALOG_MAIL:
                ((AlertDialog) dialog).setMessage(showErrors(errors));
                break;
        }
    }

}